package com.woniu.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.user.commons.Result;
import com.woniu.user.controller.form.SearchForm;
import com.woniu.user.exception.WebException;
import com.woniu.user.exception.WebExceptionCode;
import com.woniu.user.model.Account;
import com.woniu.user.model.Book;
import com.woniu.user.service.AccountService;
import com.woniu.user.service.BookService;
import com.woniu.user.service.dto.CoachInfoDto;
import com.woniu.user.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 预约信息表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {
    @Resource
    private BookService bookService;

    @Resource
    private AccountService accountService;

    @GetMapping("my")
    public Result checkMy(String condition, Integer current, Integer size, HttpServletRequest req) {
        Integer userId = JwtUtil.getUserIdFromReq(req);// 获取教练账号
        log.info("查看我的预约列表{}", userId);
        IPage<Book> bookPage = bookService.searchMy(condition, userId, current, size);
        return Result.success(200, "查询成功", bookPage);
    }

    @PostMapping("refuse")
    public Result refuseBook(Integer bookId, String message) {
        log.info("教练拒绝了预约{}, {}", bookId, message);
        bookService.refuse(bookId, message);
        return Result.success(200, "拒绝成功", null);
    }

    @PostMapping("access")
    public Result accessBook(Integer bookId) {
        log.info("教练接受了预约{}", bookId);
        bookService.access(bookId);
        return Result.success(200, "接受成功", null);
    }

    @GetMapping("check")
    public Result checkByDate(String date) {
        log.info("查看预约列表");
        List<Book> bookList = bookService.find(date);
        return Result.success(200, "查询成功", bookList);
    }

    // 学员查看自己的预约记录
    @GetMapping("getMyBook")
    public Result getMyBook(Integer page, Integer size, HttpServletRequest req){
        log.info("学员查看自己的预约记录{}", req);
        Page<Book> bookPage = new Page<>(page, size);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        IPage<Book> myBooks =  bookService.getMyBook(userId, bookPage);
        return Result.success(200, "学员查看自己的预约记录", myBooks);
    }

    // 学员取消预约
    @DeleteMapping("del")
    public Result del(Integer bookId, HttpServletRequest req){
        log.info("学员取消预约{}", bookId);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        bookService.del(bookId, userId);
        return Result.success(200, "取消成功", null);
    }

    // 关键词搜索
    @GetMapping("search")
    public Result search(SearchForm form, HttpServletRequest req){
        log.info("搜索预约单{}", form);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        IPage<Book> myBooks = null;
        if(form.getKey() == null || "".equals(form.getKey())){
            Integer page = form.getPage();
            Integer size = form.getSize();
            Page<Book> bookPage = new Page<>(page, size);
            myBooks = bookService.getMyBook(userId, bookPage);
            return Result.success(200, "搜索成功", myBooks);
        }
        myBooks =  bookService.search(form, userId);
        return Result.success(200, "搜索成功", myBooks);
    }
}

